package com.nwpu.coursesupport.service.imple;

import com.nwpu.coursesupport.criteria.ScorePerformanceSearchCriteria;
import com.nwpu.coursesupport.entity.ScorePerformance;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.Status;
import com.nwpu.coursesupport.enumeration.SubmitTaskStatus;
import com.nwpu.coursesupport.enumeration.TaskType;
import com.nwpu.coursesupport.mapper.ScorePerformanceMapper;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.tool.ServiceUtil;
import com.nwpu.coursesupport.view.TaskDetailStuView;
import com.nwpu.coursesupport.view.criteria.TaskDetailStuViewSearchCriteria;
import com.nwpu.coursesupport.view.mapper.SCCViewMapper;
import com.nwpu.coursesupport.view.mapper.TaskDetailStuViewMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TaskDetailStuViewService extends AbstractService {

    @Autowired
    private TaskDetailStuViewMapper taskDetailStuViewMapper;
    @Autowired
    private SCCViewMapper sCCViewMapper;
    @Autowired
    private ScorePerformanceMapper scorePerformanceMapper;

    protected static Logger logger = LoggerFactory.getLogger(TaskDetailStuViewService.class);


    public JSONResult taskMainDetail(IDParam p) {
        JSONResult rst = new JSONResult();
        rst.fail();
        if (p == null || p.getId() == null) {
            logger.error("参数有误");
            return rst.fail("参数有误");
        }

        int id = (Integer) getSession().getAttribute("id");
        TaskDetailStuViewSearchCriteria sc = new TaskDetailStuViewSearchCriteria();
        sc.setAssignId(p.getId());
        sc.setStuId(id);
        sc.setIsVisible(Status.YES);
        sc.setType(TaskType.TASKMAIN);
        TaskDetailStuView view = this.taskDetailStuViewMapper.taskMainDetail(sc);

        if (view == null) {
            logger.error("该任务不存在");
            return rst.fail("该任务不存在");
        }
        Date date = new Date();
        //如果已过任务截止时间或者已经被批改
        if (view.getDeadline().before(date) || view.getSubmitResult().getStatus() == SubmitTaskStatus.HAVEREAD) {
            view.setCanntSubmit(Status.YES);
        } else {//既未过期又未被批改
            view.setCanntSubmit(Status.NO);
        }
        view.setAttachment(ServiceUtil.DeserializeFiles(view.getAttachmentBytes()));
        if (view.getIsAnswerVisible() == Status.YES) {
            view.setAnswerAttachment(ServiceUtil.DeserializeFiles(view.getAnswerAttachmentBytes()));
        }
        if (view.getSubmitResult().getStatus().ordinal() >= SubmitTaskStatus.COMMITTED.ordinal()) {
            //关于提交内容的信息置空
            //DeserializeFilesForSafe将传出的文件参数的url置空
            view.getSubmitResult().setResultAttachment(ServiceUtil.DeserializeFilesForSafe(view.getSubmitResult().getResultAttachmentBytes()));
            view.getSubmitResult().setResultAttachmentBytes(null);
        }

        rst.addData("view", view);
        rst.success();
        return rst;
    }

    public JSONResult taskInLessonDetail(IDParam p) {
        JSONResult rst = new JSONResult();
        rst.fail();
        if (p == null || p.getId() == null) {
            logger.error("参数有误");
            return rst.fail("参数有误");
        }

        TaskDetailStuViewSearchCriteria sc = new TaskDetailStuViewSearchCriteria();
        sc.setAssignId(p.getId());
        sc.setType(TaskType.ASKINLESSON);
        sc.setIsVisible(Status.YES);
        TaskDetailStuView view = this.taskDetailStuViewMapper.taskInLessonDetail(sc);
        if (view == null) {
            logger.error("该任务不存在");
            return rst.fail("该任务不存在");
        }
        view.setCanntSubmit(Status.YES);
        view.setAttachment(ServiceUtil.DeserializeFiles(view.getAttachmentBytes()));
        if (view.getIsAnswerVisible() == Status.YES) {
            view.setAnswerAttachment(ServiceUtil.DeserializeFiles(view.getAnswerAttachmentBytes()));
        }

        int id = (Integer) getSession().getAttribute("id");
        ScorePerformanceSearchCriteria spsc = new ScorePerformanceSearchCriteria();
        spsc.setAssignId(p.getId());
        spsc.setAskStuId(id);
        List<ScorePerformance> sp = this.scorePerformanceMapper.find(spsc);
        if (sp.size() <= 0 || sp.size() > 1) {
            logger.error("获取课堂提问评分信息失败");
            return rst.fail("获取课堂提问评分信息失败");
        }
        rst.addData("view", view);
        rst.addData("isScore", sp.get(0).getIsScore());
        rst.addData("mark", sp.get(0).getMark());

        rst.success();
        return rst;
    }

    public JSONResult taskPersonalDetail(IDParam p) {
        JSONResult rst = new JSONResult();
        rst.fail();
        if (p == null || p.getId() == null) {
            logger.error("参数有误");
            return rst.fail("参数有误");
        }

        int id = (Integer) getSession().getAttribute("id");
        TaskDetailStuViewSearchCriteria sc = new TaskDetailStuViewSearchCriteria();
        sc.setAssignId(p.getId());
        sc.setStuId(id);
        sc.setType(TaskType.PERSONALTASK);

        TaskDetailStuView view = this.taskDetailStuViewMapper.taskPersonalDetail(sc);
        if (view == null) {
            logger.error("该任务不存在");
            return rst.fail("该任务不存在");
        }
        //如果已过任务截止时间或者已经被批改
        if (view.getSubmitResult().getStatus() == SubmitTaskStatus.HAVEREAD) {
            view.setCanntSubmit(Status.YES);
        } else {//既未过期又未被批改
            view.setCanntSubmit(Status.NO);
        }
        view.setAttachment(ServiceUtil.DeserializeFiles(view.getAttachmentBytes()));
        if (view.getSubmitResult().getIsAnswerShow() == Status.YES) {
            view.setAnswerAttachment(ServiceUtil.DeserializeFiles(view.getAnswerAttachmentBytes()));
        }
        if (view.getSubmitResult().getStatus().ordinal() >= SubmitTaskStatus.COMMITTED.ordinal()) {
            view.getSubmitResult().setResultAttachment(ServiceUtil.DeserializeFiles(view.getSubmitResult().getResultAttachmentBytes()));
        }

        rst.addData("view", view);
        //rst.addData("courseId", view.getCoureseId());
        rst.success();
        return rst;
    }
}
